package cn.yoyo.controller.fore;

import cn.yoyo.entity.Order;
import cn.yoyo.entity.OrderInfo;
import cn.yoyo.entity.SysUser;
import cn.yoyo.tools.GetUser;
import cn.yoyo.tools.RespJson;
import cn.yoyo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ForeOrderController {
    @Autowired
    OrderService orderService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    @RequestMapping("/api/fore/order/findOrdLineByPage")
    public Map<String, Object> findOrdLineByPage(@RequestBody Map<String, Object> map) {
        SysUser user = GetUser.getUser();
        if (user == null) {
            return RespJson.pack(401, "Token过期,请重新登录", null);
        }
        Integer ispay = (Integer) map.get("ispay");
        if (ispay != 0 && ispay != 1) {
            return RespJson.pack(500, "上传参数有误", null);
        }
        List<Order> orders = orderService.findOrdLineByPage(map);
        System.out.println(orders);
        if (orders.size() == 0) {
            return RespJson.pack(500, "该用户暂无订单", null);
        }
        Map<String, Object> map1 = new HashMap<>();
        map1.put("total", orderService.getTotal(new HashMap<>()));
        map1.put("orders", orders);
        return RespJson.pack(200, "", map1);
    }

    @RequestMapping("/api/fore/order/findItemInfoByOrdid/{ordid}")
    public Map<String, Object> findItemInfoByOrdid(@PathVariable("ordid") Integer ordid) {
        SysUser user = GetUser.getUser();
        if (user == null) {
            return RespJson.pack(401, "Token过期,请重新登录", null);
        }
        if (ordid <= 0) {
            return RespJson.pack(500, "上传参数有误", null);
        }
        OrderInfo orderInfos = orderService.findItemInfoByOrdid(ordid);
        System.out.println(orderInfos.toString());
        if (orderInfos == null) {
            return RespJson.pack(500, "查询数据有误", null);
        }
        return RespJson.pack(200, "", orderInfos);
    }

    @RequestMapping("/api/fore/order/addAddress")
    public Map<String, Object> addAddress(String address, String phone, Integer ordid) {
        System.out.println(address);
        System.out.println(phone);
        System.out.println(ordid);
        return RespJson.pack(200, "添加地址成功", orderService.addAddress(address, phone, ordid));
    }

    @RequestMapping("/api/fore/order/addPay/{ordid}")
    public Map<String, Object> addPay(@PathVariable("ordid") Integer ordid) {
        orderService.addPay(ordid);
        return RespJson.pack(200, "支付成功", null);
    }

    @RequestMapping("/api/fore/order/findInfoByOrdid/{ordid}")
    public Map<String, Object> findInfoByOrdid(@PathVariable("ordid") Integer ordid) {
        return RespJson.pack(200, "", orderService.findInfoByOrdid(ordid));
    }
}
